package com.pai4j.pay.messagequeue;

import com.pai4j.common.enums.messagequeue.MessageQueueEnum;
import com.pai4j.common.service.messagequeue.consumer.AbstractRedisDelayQueueConsumer;
import com.pai4j.common.util.JsonUtil;
import com.pai4j.domain.dto.queue.PaymentCallBackQueueDTO;
import com.pai4j.pay.service.PaymentCallBackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

@Service
public class PaymentCallBackRedisDelayQueue extends AbstractRedisDelayQueueConsumer<PaymentCallBackQueueDTO> {


    @Autowired
    private PaymentCallBackService paymentCallBackService;

    @Override
    protected PaymentCallBackQueueDTO convert(String s) {

        return JsonUtil.fromJson(s, PaymentCallBackQueueDTO.class);
    }

    @Override
    protected MessageQueueEnum getMessageQueue() {
        return MessageQueueEnum.QUEUE_PAYMENT_CALL_BACK_TASK_MESSAGE;
    }

    @Override
    protected void doNextConsume(Set<String> messages) {
        List<PaymentCallBackQueueDTO> list = messages.stream().map(this::convert).toList();
        paymentCallBackService.doBatchCallBack(list);
    }
}
